package com.ujoin.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ujoin.bean.User;
import com.ujoin.dao.IUserDAO;
import com.ujoin.util.HashPassword;
import com.ujoin.util.JsonUtil;

/**
 * 
 * @author 幽蓝
 *
 */

public class UserService {
	@Autowired
	private IUserDAO userDao;

	/**
	 * 用户注册
	 * @param user 用户传入的User对象
	 * @return 生成的user记录
	 */
	public String register(User user){//register
		if (!userDao.findByEmail(user.getEmail()).isEmpty()) {
			JsonUtil jo = JsonUtil.getError().setMessage("对不起，该账号已被申请");
			return jo.toJSONString();			
		};
		user.setPassword(HashPassword.encodePassword(user.getPassword()));
		user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
		user.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
		userDao.save(user);
		JsonUtil jo = JsonUtil.setAllComponentsName(userDao
				.findById(user.getUserId())).setSuccess();
		return jo.toJSONString();
	}

	/**
	 * 用户登录
	 * @param user 用户传入的User对象
	 * @return 结果Json
	 */
	public String login(User user){
		List userList = userDao.findByEmail(user.getEmail());
		if (userList.isEmpty()) {
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("没有这个账号，请核实！");
			return jo.toJSONString();
		}
		User u = (User) userList.get(0);
		if (u.getPassword().equals(HashPassword.encodePassword(user.getPassword()))){
			u.setPassword("");
			JsonUtil jo = JsonUtil.setAllComponentsName(u).setSuccess();
			return jo.toJSONString();
		} else {
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("账号或密码错误");
			return jo.toJSONString();
		}
	}
	
	public String saveUser(User user){//complete imformation
		User u = (User) userDao.findByEmail(user.getEmail());
		if(u == null){
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("没有这个账号，请核实！");
			return jo.toJSONString();
		}
		else{
			userDao.save(user);
			JsonUtil jo = JsonUtil.setAllComponentsName(userDao
					.findById(user.getUserId())).setSuccess();
			return jo.toJSONString();
		}
	}
	
	public String findUserByEmail(User user)//find by email
	{
		User a = (User) userDao.findByEmail(user.getEmail());
		JsonUtil jo = JsonUtil.setAllComponentsName(a).setSuccess();
		return jo.toJSONString();
	}

	public String findAll()//find imformation
	{
		List list = userDao.findAll();
		if (list.isEmpty())
		{
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("无记录！");
			return jo.toJSONString();
		}
		else
		{
			JsonUtil jo = JsonUtil.setComponentList(list).setSuccess();
			return jo.toJSONString();
		}
	}

	public String updateUser(User user)//modify imformation
	{
		if (user.getEmail() == null)
		{
			JsonUtil jo = JsonUtil.getError();
			jo.setMessage("账号不存在，请核实！");
			return jo.toJSONString();
		}
		else{
		userDao.attachDirty(user);
		JsonUtil jo = JsonUtil.setAllComponentsName(userDao.findById(user
				.getUserId())).setSuccess();
		return jo.toJSONString();
		}
	}

	public String removeUser(User user)
	{
		User u = (User) userDao.findByEmail(user.getEmail());
		userDao.delete(u);
		JsonUtil jo = JsonUtil.getSuccess();
		return jo.toJSONString();
	}
}
